<?php
session_start();
require_once("../config/include_tokyo.inc.php");

//访问权限验证
$userId = $_SESSION['user_id'];
$userRole = $_SESSION['user_role'];
if($userRole == 1 ||  $userRole == 5){
	//管理员,订单管理员,任务管理员+订单管理员
}else{
	if($userRole > 0){
		$url = "../index.php?loginError=2";//没权限
	}else{
		$url = "../index.php?loginError=3";//没登录
	}	
	echo "<script language='javascript' type='text/javascript'>";
	echo "window.location.href='$url'";
	echo "</script>";
}


//项目详情 
$id = $_GET['id'];
if($id == ""){
	//新增项目
}else{
	//编辑项目
	$sql = "SELECT * from projects where id=".$id;
	$result = $db->Execute($sql);
}



$doFlag = $_POST["doFlag"];
if($doFlag == "insertProject"){
	//保存
	$project_name = $_POST["project_name"];
	$project_description = $_POST["project_description"];
	$project_plan = $_POST["project_plan"];
	$project_risk = $_POST["project_risk"];
	$begin_time = $_POST["begin_time"];
	$expect_end_time = $_POST["expect_end_time"];
	$project_level = $_POST["project_level"];
	$project_status = $_POST["project_status"];
	$project_owner = $_POST["project_owner"];
	$sql_insert_projects = "INSERT INTO projects
							 (project_name,project_description,project_plan,project_risk,begin_time,expect_end_time,project_level,project_status,project_owner) 
							 values
							 ('$project_name','$project_description','$project_plan','$project_risk','$begin_time','$expect_end_time',$project_level,$project_status,$project_owner)";
	$db->Execute($sql_insert_projects);	
}else if($doFlag == "updateProject"){
	//更新
	$projectId = $_POST["projectId"];
	$project_name = $_POST["project_name"];
	$project_description = $_POST["project_description"];
	$project_plan = $_POST["project_plan"];
	$project_risk = $_POST["project_risk"];
	$begin_time = $_POST["begin_time"];
	$expect_end_time = $_POST["expect_end_time"];
	$project_level = $_POST["project_level"];
	$project_status = $_POST["project_status"];
	$project_owner = $_POST["project_owner"];
	$project_summary = $_POST["project_summary"];

	$sql = "update projects set project_name = '".$project_name."'";
	$sql = $sql.", project_description='".$project_description."'";
	$sql = $sql.", project_plan='".$project_plan."'";
	$sql = $sql.", project_risk='".$project_risk."'";
	$sql = $sql.", begin_time='".$begin_time."'";
	$sql = $sql.", expect_end_time='".$expect_end_time."'";
	$sql = $sql.", project_level=".$project_level;
	$sql = $sql.", project_status=".$project_status;
	$sql = $sql.", project_summary='".$project_summary."'";
	if($project_status != 1){
		$sql = $sql.", close_time=NOW() ";
	}
	if($project_status!=1 ){
	}
	$sql = $sql."  where id=".$projectId;	
	$db->Execute($sql);
}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<!--Begin:head-->
<? $pageTitle = "项目详情"; ?>
<?php include("../head.php"); ?>
<!--End:head-->


<body>
<?php include("../topMenu.php"); ?>


<form name="inputForm" action="../project/projectInput.php" method="post">
<div id="container" >	
<div class="grayTable">	
<h4>
<?php
if($doFlag == "updateProject"){
	echo "项目内容更新成功!";
}else{
	echo "项目详情";
}
?>
</h4>
<table >
<thead>
	<tr>
		<th scope="col" width=90 >字段名称</th>
	    <th scope="col" >字段值</th>		
	</tr>
</thead>

<?php
if($id == ""){
	$strTR = "";
	$strTR = $strTR."<tr ><td>项目名称</td>";
	$strTR = $strTR."<td><input type='text' name='project_name' id='project_name' value='' size=64></td></tr>";
	$strTR = $strTR."<tr ><td>项目描述</td>";
	$strTR = $strTR."<td><textarea  cols='60'  rows='5' name='project_description' id='project_description' ></textarea></td></tr>";
	$strTR = $strTR."<tr ><td>项目计划</td>";
	$strTR = $strTR."<td><textarea  cols='60'  rows='5' name='project_plan' id='project_plan' ></textarea></td></tr>";
	$strTR = $strTR."<tr ><td>风险评估</td>";
	$strTR = $strTR."<td><textarea  cols='60'  rows='5' name='project_risk' id='project_risk' ></textarea></td></tr>";
	$strTR = $strTR."<tr ><td>开始时间</td>";
	$strTR = $strTR."<td><input type='text' name='begin_time' id='begin_time' size=32  value='".date('Y-m-d H:i:s',time())."'></td></tr>";
	$strTR = $strTR."<tr ><td>完成时间(预期)</td>";
	$strTR = $strTR."<td><input type='text' name='expect_end_time' id='expect_end_time' size=32  value='".date('Y-m-d H:i:s',time())."'></td></tr>";
	$strTR = $strTR."<tr ><td>项目等级</td>";
	$strTR = $strTR."<td><select name='project_level' id='project_level'><option value=4 >重要紧急</option><option value=3 selected>重要</option><option value=2 >重要不紧急</option><option value=1 >普通</option></select></td></tr>";	
	$strTR = $strTR."<tr ><td>项目状态</td>";
	$strTR = $strTR."<td><select name='project_status' id='project_status'><option value=1 selected>进行中</option><option value=2>暂停</option><option value=3>已完成</option><option value=4>已失败</option><option value=5>已取消</option></select></td></tr>";	
	$strTR = $strTR."<tr><td colspan=2 align=center>";
	$strTR = $strTR."<input type='button' value='保存' onclick='insertProject()'>";
	$strTR = $strTR."<input type='button' value='返回' onClick='toList()'>";
	$strTR = $strTR."</td></tr>";
	echo $strTR;
}else{	
	if(!$result->EOF){
		//********项目等级*************
		$project_level = $result->fields['project_level'];
		if($project_level == "1"){
			$project_level_1 = "selected";
		}else if($project_level == "2"){
			$project_level_2 = "selected";
		}if($project_level == "3"){
			$project_level_3 = "selected";
		}if($project_level == "4"){
			$project_level_4 = "selected";
		}
		//********项目状态*************
		$project_status = $result->fields['project_status'];
		if($project_status == "1"){
			$project_status_1 = "selected";
		}else if($project_status == "2"){
			$project_status_2 = "selected";
		}else if($project_status == "3"){
			$project_status_3 = "selected";
		}else if($project_status == "4"){
			$project_status_4 = "selected";
		}else if($project_status == "5"){
			$project_status_5 = "selected";
		}
		//********项目项目负责人*************
		$user_sql = "SELECT * from users where id=".$result->fields['project_owner'];
		$user_result = $db->Execute($user_sql);
		if(!$user_result->EOF){
			$userName = $user_result->fields['user_name'];
		}
		//********Display*************
		$strTR = "";
		$strTR = $strTR."<tr ><td>项目编号</td>";
		$strTR = $strTR."<td>".$result->fields['id']."</td></tr>";
		$strTR = $strTR."<tr ><td>创建时间</td>";
		$strTR = $strTR."<td>".$result->fields['create_time']."</td></tr>";
		$strTR = $strTR."<tr ><td>项目名称</td>";
		$strTR = $strTR."<td><input type='text' name='project_name' id='project_name' value='".$result->fields['project_name']."' size=64></td></tr>";
		$strTR = $strTR."<tr ><td>项目描述</td>";
		$strTR = $strTR."<td><textarea  cols='60'  rows='5' name='project_description' id='project_description' >".$result->fields['project_description']."</textarea></td></tr>";
		$strTR = $strTR."<tr ><td>项目计划</td>";
	    $strTR = $strTR."<td><textarea  cols='60'  rows='5' name='project_plan' id='project_plan' >".$result->fields['project_plan']."</textarea></td></tr>";
		$strTR = $strTR."<tr ><td>风险评估</td>";
		$strTR = $strTR."<td><textarea  cols='60'  rows='5' name='project_risk' id='project_risk' >".$result->fields['project_risk']."</textarea></td></tr>";
		$strTR = $strTR."<tr ><td>开始时间</td>";
		$strTR = $strTR."<td><input type='text' name='begin_time' id='begin_time' size=32  value='".$result->fields['begin_time']."'></td></tr>";
		$strTR = $strTR."<tr ><td>完成时间(预期)</td>";
		$strTR = $strTR."<td><input type='text' name='expect_end_time' id='expect_end_time' size=32  value='".$result->fields['expect_end_time']."'></td></tr>";
		$strTR = $strTR."<tr ><td>项目等级</td>";
		$strTR = $strTR."<td><select name='project_level' id='project_level'><option value=4 ".$project_level_4.">重要紧急</option><option value=3 ".$project_level_3.">重要</option><option value=2 ".$project_level_2.">重要不紧急</option><option value=1 ".$project_level_1.">普通</option></select></td></tr>";	
		$strTR = $strTR."<tr ><td>项目状态</td>";
		$strTR = $strTR."<td><select name='project_status' id='project_status'><option value=1 ".$project_status_1.">进行中</option><option value=2  ".$project_status_2.">暂停</option><option value=3 ".$project_status_3.">已完成</option><option value=4  ".$project_status_4.">已失败</option><option value=5  ".$project_status_5.">已取消</option></select></td></tr>";			
		$strTR = $strTR."<tr ><td>项目总结</td>";
		$strTR = $strTR."<td><textarea  cols='60'  rows='5' name='project_summary' id='project_summary' >".$result->fields['project_summary']."</textarea></td></tr>";
		$strTR = $strTR."<tr ><td>项目负责人</td>";
		$strTR = $strTR."<td>".$userName."</td></tr>";
		$strTR = $strTR."<tr><td colspan=2 align=center>";
		$strTR = $strTR."<input type='button' value='保存' onclick='updateProject(".$id.")'>";
		$strTR = $strTR."<input type='button' value='返回' onClick='toList()'>";
		$strTR = $strTR."</td></tr>";
		echo $strTR;
	}
}
?>

</table>



</div>

<br><br>
<br><br>

</div><!--end container -->

<input type="hidden" name="doFlag" id="doFlag" >
<input type="hidden" name="projectId" id="projectId" >
<input type="hidden" name="project_owner" id="project_owner" value="<?php echo $userId;?>">
</form>
</body>




</html>

<script>
<?php
if($doFlag == "insertProject"){
	echo "window.location.href = 'projectList.php';"; 
}else if($doFlag == "updateProject"){
	echo "window.location.href = 'projectReport.php?id=".$projectId."';"; 
}

?>

function toList(){
	window.location.href = "projectList.php";
}


function insertProject(){
	document.getElementById('doFlag').value = 'insertProject';	
	document.inputForm.submit();
}

function updateProject(id){
	document.getElementById('doFlag').value = 'updateProject';	
	document.getElementById('projectId').value = id;
	document.inputForm.submit();
}


</script>

